package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    //-1--新增套餐--清缓存
    @CacheEvict(value = "setmealCache",key = "'setmeal_'+#a0.categoryId")
    @PostMapping()
    @ApiOperation("根据分类id查询菜品 ")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setmealService.insertOne(setmealDTO);
        return Result.success();
    }

    //-2--套餐分页
    @GetMapping("/page")
    @ApiOperation("套餐分页 ")
    public Result<PageResult> selectPage(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setmealService.selectPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    //-3--删除套餐--清缓存
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping()
    @ApiOperation("删除套餐 ")
    public Result deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return Result.success();
    }

    //-4.1--根据id查询套餐   /admin/setmeal/{id}
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐 ")
    public Result<SetmealVO> selectById(@PathVariable Long id){
        SetmealVO setmeal=setmealService.selectById(id);
        return Result.success(setmeal);
    }
    //-4.2--修改套餐--清缓存
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping()
    @ApiOperation("修改套餐 ")
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    //-5--套餐起售、停售--清缓存  /admin/setmeal/status/{status}
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售 ")
    public Result updateSetmealByStatus(@PathVariable Integer status,Long id){
        setmealService.updateSetmealByStatus(status,id);
        return Result.success();
    }




}
